﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sweepids.Models.Dao;
using Sweepids.Models.Object;

namespace Sweepids.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string username, string password)
        {
            try
            {
                if (username.Equals("") || password.Equals(""))
                {
                    ViewBag.Error = "Invalid username or password.";
                    return View();
                }

                var userDao = new UserDao();
                var user = userDao.Read(username);

                if (user.FirstName.Equals("") || !user.Password.Equals(password))
                {
                    ViewBag.Error = "Invalid username or password.";
                    return View();
                }

                Session["User"] = user;
                user.Fill();
                return RedirectToAction("Index", user.IsAdmin ? "ManageUsers" : "Home");
            }
            catch (Exception)
            {
                TempData["Error"] = "Something went wrong logging in, sorry.";
                return RedirectToAction("Index", "Login");
            }
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult RegisterUser(string username, string password, string firstname, string lastname, string email, string address, string city, string state, string zip, string cardNumber, string expDate, string secCode)
        {
            try
            {
                var userDao = new UserDao();
                var zipDao = new ZipDao();
                var ccDao = new CreditCardDao();

                var dbZip = zipDao.Read(zip);

                // new zipcode
                if (dbZip.City.Equals(""))
                {
                    zipDao.Create(new Zip { City = city, State = state, ZipId = zip });
                }
                else if (!dbZip.City.Equals(city) || !dbZip.State.Equals(state))
                {
                    // problem, zip doesn't correspend to the state and city they entered in
                    TempData["Error"] = "The zip code did not match the city/state.";
                    return RedirectToAction("Register", "Login");
                }

                var dbCreditCard = ccDao.Read(cardNumber);

                // new card
                if (dbCreditCard.SecCode.Equals(""))
                {
                    ccDao.Create(new CreditCard { CardNum = cardNumber, SecCode = secCode, ExpDate = DateTime.Parse(expDate) });
                }
                else if (!dbCreditCard.SecCode.Equals(secCode))
                {
                    TempData["Error"] = "The credit card information does not match our records.";
                    return RedirectToAction("Register", "Login");
                }

                var newUser = new User
                {
                    FirstName = firstname,
                    LastName = lastname,
                    Address = address,
                    CreditCard = cardNumber,
                    Email = email,
                    IsAdmin = false,
                    SweepsOwned = 0,
                    Username = username,
                    Password = password,
                    Zip = zip,
                };

                var dbUser = userDao.Read(newUser.Username);
                if (dbUser.FirstName.Equals(""))
                {
                    userDao.Create(newUser);
                }
                else
                {
                    TempData["Error"] = "User already exists";
                    return RedirectToAction("Register", "Login");
                }


                TempData["Success"] = "Account created.";
                return RedirectToAction("Index", "Login");
            }
            catch (Exception e)
            {
                TempData["Error"] = "Something went wrong.";
                return RedirectToAction("Register", "Login");
            }
        }

        public ActionResult Logout()
        {
            Session["User"] = null;
            return RedirectToAction("Index", "Login");
        }

    }
}
